//
//  ViewController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "HYCHeadDocument.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height



@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) MBProgressHUD *hud;
@end

@implementation ViewController
- (void)dealloc {
    [_collectionView release];
    [_arr release];
    [_hud release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
    self.title = @"订酒店";
    
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"sou.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    
    
    UIImage *beanTripBackgroundImage=[UIImage imageNamed:@"tou.jpg"];
    [self.navigationController.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2.5, HEIGHT / 4.67);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 25, 10, 25);

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 108) collectionViewLayout:flowLayout];
    //    self.tableView.tableHeaderView = self.collectionView;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[HYCPudCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    

    //设置
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 18);
    [button setBackgroundImage:[UIImage imageNamed:@"shezhi.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  
    [self netWorkingJudge];
}

- (void)netWorkingJudge {
    
    AFNetworkReachabilityManager *judge = [AFNetworkReachabilityManager sharedManager];
    [judge startMonitoring];
    [judge setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 1 || status == 2) {
            [self createData];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"没有网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertView show];
        }
        
    }];
    
}

// 设置
- (void) buttonAction
{
    HYCheyanchaoViewController *HYCVC = [[HYCheyanchaoViewController alloc] init];
    HYCVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:HYCVC animated:YES];
    
}

- (void)openOrCloseLeftList
{
    HYCSeacrhBarController *HYCVC = [[HYCSeacrhBarController alloc] init];
    HYCVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:HYCVC animated:YES];
    
}
- (void)createData {
    NSString *str = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=9&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [HYCPudModel baseModelByArr:dic[@"data"]];
        [self.collectionView reloadData];
        [self.hud removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HYCPudController *pVC = [[HYCPudController alloc] init];
    pVC.hidesBottomBarWhenPushed = YES;
    pVC.str = [self.arr[indexPath.row] id];
    NSLog(@"%@", pVC.str);
    [self.navigationController pushViewController:pVC animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HYCPudCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (self.arr.count != 0) {
        
        cell.label.text = [self.arr[indexPath.row] cnname];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"zw.png"]];
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
        
    }

    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
